# -*- coding: utf-8 -*-
# flake8: noqa
import uuid

from qiniu import Auth, put_file, etag
import qiniu.config



#需要填写你的 Access Key 和 Secret Key 步骤3详细说明
access_key = '6YixLhlCp-WNBBQjYHPRBiSKtKdtVhtIgv0YoqlV'
secret_key = 'd7fvWuxEGUA0uo_e_z0l4shcF75bSqmBfXGUPtzW'


# 生成上传凭证
def qiniu_token(bucked_name, key):

    q = Auth(access_key = access_key,
             secret_key = secret_key)
    token = q.upload_token(bucked_name, key)
    return token

def upload_img(file_path):  # file_path
    """
    收集本地信息到云服务器上
    参考地址：https://developer.qiniu.com/kodo/sdk/1242/python
    """
    bucked_name = 'ihome-team2'
    # 指定图片名称,上传后保存的文件名
    file_name = '{}.png'.format(uuid.uuid4())
    # 指定上传空间，获取token
    token = qiniu_token(bucked_name, file_name)

    # file_path = '/home/ubuntu/Desktop/demo.png'
    ret, info = put_file(token, file_name, file_path)

    img_key = ret.get('key')
    return img_key

# # 测试
# if __name__ == '__main__':
#     # bucked_name = 'ihome-team2'
#     # file_path = '/home/ubuntu/Desktop/demo.png'
#     # domain_name = 'http://qtsrimd9d.hn-bkt.clouddn.com/'
#     img_url = upload_img()
#     print(img_url)  # http://qtsrimd9d.hn-bkt.clouddn.com/7c0d346b-fd56-4984-80ec-2998d3daf690.png






# sdk文档
#
#
# #构建鉴权对象
# q = Auth(access_key, secret_key)
#
# #要上传的空间 步骤4详细说明
# bucket_name = 'ihome-team2'
#
# #上传到七牛后保存的文件名
# # key = str(hash("logo.png")) + ".png"
# key = 'demo.png'
#
# #生成上传 Token，可以指定过期时间等
# token = q.upload_token(bucket_name, key, 3600)
#

# #要上传文件的本地路径（就是你文件存放在本地盘的什么位置）
# localfile = '/home/ubuntu/Desktop/demo.png'
#
# ret, info = put_file(token, key, localfile)
# print(info)
# # assert ret['key'] == key
# # assert ret['hash'] == etag(localfile)
# # # _ResponseInfo__response:<Response [200]>, exception:None, status_code:200, text_body:{"hash":"FjE0k8sYsNUeG0tpmZ0cS14IxAJE","key":"demo.png"}, req_id:DNYAAABiT9yZJoMW, x_log:X-Log
#
# # 拼接路径   qtsrimd9d.hn-bkt.clouddn.com这个是创建空间分配的测试域名
# image_file = 'http://qtsrimd9d.hn-bkt.clouddn.com/' + ret.get('key')
# print(image_file)   # http://qtsrimd9d.hn-bkt.clouddn.com/demo.png
